package net.dodoman.douniu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 斗牛程序
 * 
 * @author yanping
 * 
 */
public class DouNiuApp {

	/**
	 * 整副牌(除去大小王 52张)
	 */
	private List<Card> cards = new ArrayList<Card>(52);

	/**
	 * 已发出的牌
	 */
	private Map<String, Card> cardsOutYet = new HashMap<String, Card>();

	/**
	 * 初始化四个玩家
	 */
	private Player p1 = new Player(), p2 = new Player(), p3 = new Player(),
			p4 = new Player();

	public static void main(String[] args) {
		DouNiuApp dnapp = new DouNiuApp();
		dnapp.init();
		dnapp.cardsOut(dnapp.p1);
		dnapp.cardsOut(dnapp.p2);
		dnapp.cardsOut(dnapp.p3);
		dnapp.cardsOut(dnapp.p4);
		int p1score = dnapp.score(dnapp.p1);
		// int p2score = dnapp.score(dnapp.p2);
		// int p3score = dnapp.score(dnapp.p3);
		// int p4score = dnapp.score(dnapp.p4);
		System.out.print("p1的牌:");
		for (Card c : dnapp.p1.getHaveCards()) {
			System.out.print(c.getSuit() + c.getFace() + "|");
		}
		System.out.print("\tp1得分:" + p1score + "\n");
		// System.out.print("p2的牌:");
		// for (Card c : dnapp.p2.getHaveCards()) {
		// System.out.print(c.getFace() + c.getSuit());
		// }
		// System.out.print("\tp2得分:" + p2score + "\n");
		// System.out.print("p3的牌:");
		// for (Card c : dnapp.p3.getHaveCards()) {
		// System.out.print(c.getFace() + c.getSuit());
		// }
		// System.out.print("\tp3得分:" + p3score + "\n");
		// System.out.print("p4的牌:");
		// for (Card c : dnapp.p4.getHaveCards()) {
		// System.out.print(c.getFace() + c.getSuit());
		// }
		// System.out.print("\tp4得分:" + p4score + "\n");
	}

	/**
	 * 初始化牌
	 */
	public void init() {
		initDeck();
	}

	/**
	 * 发牌(算法待改进)
	 */
	public void cardsOut(Player p) {
		// 对4个人发牌,一共发20张
		Random random = new Random();
		int rnd = random.nextInt(52);
		Card card = cards.get(rnd);
		String key = card.getFace() + card.getSuit();
		List<Card> phcs = p.getHaveCards();
		if (null == cardsOutYet.get(key)) {
			phcs.add(card);
			p.setHaveCards(phcs);
			cardsOutYet.put(key, card);
		}
		int pcardssize = phcs.size();
		if (pcardssize >= 5) {
			return;
		} else {
			cardsOut(p);
		}
	}

	/**
	 * 计算玩家的最后得分
	 * 
	 * @param p
	 * @return
	 */
	public synchronized int score(Player p) {
		// 这里的算法忽略花色
		// 其中三张牌的顺序加起来是10的倍数,花牌的值都等于10
		List<Card> pcards = p.getHaveCards();
		Map<String, String> hascalc = new HashMap<String, String>();
		boolean hasanswner = false;
		int value = 0;
		for (Card c1 : pcards) {
			for (Card c2 : pcards) {
				for (Card c3 : pcards) {
					String c1f = c1.getFace(), c2f = c2.getFace(), c3f = c3
							.getFace();
					String c1s = c1.getSuit(), c2s = c2.getSuit(), c3s = c3
							.getSuit();
					if (!(c1f.equals(c2f)) && !(c2f.equals(c3f))
							&& !(c3f.equals(c1f))) {
						if (null == hascalc.get(c1f)
								&& null == hascalc.get(c2f)
								&& null == hascalc.get(c3f)) {
							int total = getCardValue(c1) + getCardValue(c2)
									+ getCardValue(c3);
							if (total % 10 == 0) {
								hascalc.put(c1s + c1f, c1f);
								hascalc.put(c2s + c2f, c2f);
								hascalc.put(c3s + c3f, c3f);
								System.out.println("--------------------");
								System.out.println("有解的三张牌:");
								System.out.println(c1f + "|" + c2f + "|" + c3f);
								System.out.println("--------------------");
								hasanswner = true;
								break;
							}
						}
					}
				}
				if (hasanswner) {
					break;
				}
			}
			if (hasanswner) {
				break;
			}
		}
		if (hasanswner) {
			for (Card c : pcards) {
				if (null == hascalc.get(c.getSuit() + c.getFace())) {
					value += getCardValue(c);
				}
			}
		}
		return value;
	}

	/**
	 * 
	 * @param c
	 * @return
	 */
	public int getCardValue(Card c) {
		String face = c.getFace();
		if ("A" == face) {
			return 1;
		} else if ("J".equals(face) || "Q".equals(face) || "K".equals(face)) {
			return 10;
		} else {
			return Integer.parseInt(c.getFace());
		}
	}

	/**
	 * 获得洗好后的牌
	 */
	private void initDeck() {
		Card[] deck = new Card[52];
		String f[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
				"Q", "K" };
		String s[] = { "黑桃", "红心", "梅花 ", "方块" };
		for (int i = 0; i < s.length; i++) {
			for (int j = 0; j < f.length; j++) {
				deck[i * 13 + j] = new Card(s[i], f[j]);
			}
		}
		for (int i = 0; i < deck.length; i++) {
			cards.add(deck[i]);
		}
	}

}
